package com.gnerv.sylvanas.framework.common.tools;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

/**
 * @author Gnerv LiGen
 */
public class ResourceTools {

    private ResourceTools() {
    }

    public static InputStream getResourceStream(String name) {
        return ResourceTools.class.getClassLoader().getResourceAsStream(name);
    }

    public static File getResourceFile(String name) throws IOException {
        InputStream inputStream = null;
        File tempFile;
        try {
            inputStream = getResourceStream(name);
            tempFile = File.createTempFile(UUIDTools.lower(), null);
            Files.copy(inputStream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        } finally {
            assert inputStream != null;
            inputStream.close();
        }
        return tempFile;
    }

}
